package com.tgg.treeproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tgg.treeproject.pojo.TreeData;
import com.tgg.treeproject.mapper.TreeDataMapper;
import com.tgg.treeproject.service.TreeDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgg.treeproject.utils.TreeMenuUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 田国刚
 * @since 2023-04-21
 */
@Service
public class TreeDataServiceImpl extends ServiceImpl<TreeDataMapper, TreeData> implements TreeDataService {
    @Resource
    TreeDataService treeDataService;
    @Override
    public HashMap<String, Object> getTreeDataForTree() {
        List<TreeData> list = treeDataService.list();
        List<TreeData> treeMenuList = TreeMenuUtil.getTreeMenuList(list, "0");
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",treeMenuList);
        map.put("code",200);
        map.put("message","获取成功");
        return map;
    }

    @Override
    public HashMap<String, Object> saveTreeDataForTree(TreeData treeData) {
        boolean save = treeDataService.save(treeData);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",save);
        map.put("code",200);
        map.put("message","存储成功");
        return map;
    }

    @Override
    public HashMap<String, Object> updateTreeDataForTree(TreeData treeData) {
        UpdateWrapper<TreeData> treeDataUpdateWrapper = new UpdateWrapper<>();
        treeDataUpdateWrapper.eq("id",treeData.getId());
        boolean save = treeDataService.update(treeData,treeDataUpdateWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",save);
        map.put("code",200);
        map.put("message","更新成功");
        return map;
    }

    @Override
    public HashMap<String, Object> deleteTreeDataForTree(String id) {
        UpdateWrapper<TreeData> treeDataUpdateWrapper = new UpdateWrapper<>();
        treeDataUpdateWrapper.eq("id",id);
        boolean remove = treeDataService.remove(treeDataUpdateWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",remove);
        map.put("code",200);
        map.put("message","删除成功");
        return map;
    }
}
